﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;
using System.IO;

namespace CompanyManagement.Controllers
{
    public class ProfileController : Controller
    {
        //
        // GET: /Profile/

        UserDLL dllUser = new UserDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProfileController));

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }

            UserMetaData UserMetaData = new UserMetaData();
            try
            {
                UserMetaData = dllUser.GetUserByID(Utility.getUserID());
                Utility.setCookiValue();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(UserMetaData);
        }

        [HttpPost]
        public ActionResult Index(UserMetaData usermetadata)
        {
            UserMetaData UserMetaData = new UserMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllUser.UpdateUser(usermetadata);
                    Utility.setCookiValue();
                }
                else
                {
                    TempData["Success"] = "Profile updated successfully.";
                    return View(usermetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(UserMetaData);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public void UploadProfilePic()
        {
            try
            {
                var file = Request.Files;
                foreach (string upload in Request.Files)
                {
                    if (Request.Files[upload].ContentLength == 0) continue;
                    string pathToSave = Server.MapPath("~/ProfilePics");
                    if (!Directory.Exists(pathToSave))
                    {
                    }
                    string filename = Path.GetFileName(Request.Files[upload].FileName);
                    Request.Files[upload].SaveAs(Path.Combine(pathToSave, filename));
                    CompanyMgtEntities db = new CompanyMgtEntities();
                    int userid = Utility.getUserID();
                    var user = db.Users.Where(u => u.UserID == userid).FirstOrDefault();
                    if (user != null)
                    {
                        user.ProfilePicPath = filename;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }

    }
}
